package com.flyaudio.xiaopengautopilot.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.flyaudio.xiaopengautopilot.R;

public class FlySeatView extends View {

    private Drawable mSeatDrawable, mSeatBackDrawable;
    private int mSeatWidth, mSeatHeight, mSeatOffSetY;
    private int mSeatBackWidth, mSeatBackHeight;

    private float moveX = 0, moveY = 0, moveZ = 0;

    public FlySeatView(Context context, AttributeSet set) {
        super(context, set);
        init(context,set);
    }

    private void init(Context context, AttributeSet attrs) {
        if (null != attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlySeatObj);
            mSeatDrawable = typedArray.getDrawable(R.styleable.FlySeatObj_mSeatCushionDrawable);
            mSeatWidth = typedArray.getInteger(R.styleable.FlySeatObj_mSeatCushionWidth, 0);
            mSeatHeight = typedArray.getInteger(R.styleable.FlySeatObj_mSeatCushionHeight, 0);
            mSeatOffSetY = typedArray.getInteger(R.styleable.FlySeatObj_mSeatCushionOffSetY, 0);

            mSeatBackDrawable = typedArray.getDrawable(R.styleable.FlySeatObj_mSeatBackDrawable);
            mSeatBackWidth = typedArray.getInteger(R.styleable.FlySeatObj_mSeatBackWidth, 0);
            mSeatBackHeight = typedArray.getInteger(R.styleable.FlySeatObj_mSeatBackHeight, 0);
            typedArray.recycle();
        }
    }

    public void setSeatMovePositon(float x, float y, float z) {
        moveX = x;
        moveY = y;
        moveZ = z;
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSeatBackBg(canvas);
        drawSeatBg(canvas);
    }

    private void drawSeatBg(Canvas canvas) {
        Bitmap mProgressThumbBitmap = DrawableToBitmap(mSeatDrawable, mSeatWidth, mSeatHeight);
        canvas.drawBitmap(mProgressThumbBitmap, moveX+mSeatBackWidth, moveY+mSeatBackHeight-mSeatHeight/2+mSeatOffSetY, null);
    }

    private void drawSeatBackBg(Canvas canvas) {
        Bitmap mProgressThumbBitmap = DrawableToBitmap(mSeatBackDrawable, mSeatBackWidth, mSeatBackHeight);
        Matrix matrix = new Matrix();
        matrix.postTranslate(-mSeatBackWidth, -mSeatBackHeight+mSeatHeight/2);
        matrix.postRotate(moveZ);
        matrix.postTranslate(mSeatBackWidth+mSeatWidth+moveX-moveZ,mSeatBackHeight+mSeatOffSetY+moveY+moveZ);
        canvas.drawBitmap(mProgressThumbBitmap, matrix, null);
        matrix.reset();
    }

    private Bitmap DrawableToBitmap(Drawable drawable, int width, int height) {
        Bitmap bitmap = null;
        if (null != drawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            bitmap = bd.getBitmap();
            int src_w = bitmap.getWidth();
            int src_h = bitmap.getHeight();
            float scale_w = ((float) width) / src_w;
            float scale_h = ((float) height) / src_h;
            Matrix matrix = new Matrix();
            matrix.postScale(scale_w, scale_h);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);
        }
        return bitmap;
    }
}